﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Struct_CityBaseInfo。
    /// </summary>
    public class Struct_CityBaseInfo
    {
        public Struct_CityBaseInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CityId,CityCode,CityName ");
            strSql.Append(" FROM Struct_CityBaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CityId,CityCode,CityName ");
            strSql.Append(" FROM Struct_CityBaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo> list = new List<Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Struct_CityBaseInfo(");
            strSql.Append("CityId,CityCode,CityName)");

            strSql.Append(" values (");
            strSql.Append("@CityId,@CityCode,@CityName)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityCode", DbType.AnsiString, model.CityCode);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo model = new Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo();
            object ojb;
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityCode = dataReader["CityCode"].ToString();
            model.CityName = dataReader["CityName"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

